﻿/// <reference path="../../ext-all-debug.js" />
Ext.define('yzmh.controller.Authors', {
    extend: 'Ext.app.Controller',

    stores: ['Authors'],

    models: ['Author'],

    views: ['author.Edit', 'author.List'],

    refs: [{
        ref: 'grid',
        selector: 'authorlist'
        }, {
            ref: 'tbar',
            selector: 'authorlist toolbar'
        }, {
            ref: 'progressbar',
            selector: 'authorlist progressbar'
        }
    ],

    init: function () {
        this.control({
            'authorlist': {
                render: this.loadAuthors,
                itemdblclick: this.editAuthor
            },
            'authorlist actioncolumn[action=tjchange]': {
                click: this.tjChange
            },
            'authorlist actioncolumn[action=view]': {
                click: this.viewAuthor
            },
            'authorlist button[action=add]': {
                click: this.addAuthor
            },
            'authorlist button[action=html]': {
                click: this.htmlAuthor
            },
            'authorlist button[action=htmlall]': {
                click: this.htmlAllAuthor
            },
            'authorlist button[action=delete]': {
                click: this.deleteAuthor
            },
            'authoredit button[action=save]': {
                click: this.saveAuthor
            }
        });
    },

    deleteAuthor: function () {
        var rows = this.getGrid().getSelectionModel().getSelection();
        var store = this.getAuthorsStore();
        var ids = [];
        if (rows.length == 0) {
            Ext.Msg.alert("提示", "请至少选择一行记录！");
            return;
        }
        Ext.each(rows, function (r) {
            ids.push(r.get("id"));
        });
        Ext.Msg.confirm("提示", "确定要删除选中的" + rows.length + "个作者吗？", function (result) {
            if (result == "yes") {
                Ext.Ajax.request({
                    url: 'data/author.aspx?a=delete',
                    params: { ids: ids.join(',') },
                    method: 'POST',
                    success: function (response, opts) {
                        var result = Ext.decode(response.responseText);
                        if (result.success) {
                            store.remove(rows);
                        } else {
                            Ext.Msg.alert("错误", result.errorMessage);
                        }
                    }
                });
            }
        });
    },

    viewAuthor: function (o, item, rowIndex, colIndex, e, row) {
        window.open("/list/author/" + row.get("id") + ".html", '_blank');
    },

    loadAuthors: function () {
        this.getTbar().insert(2, {
            store: this.getAuthorsStore(),
            xtype: 'searchfield',
            width: 320
        });
        this.getAuthorsStore().loadPage(1);
    },

    editAuthor: function (grid, record) {
        var edit = Ext.create('yzmh.view.author.Edit').show();
        edit.down('form').loadRecord(record);
        edit.down('form').load({
            url: 'data/author.aspx?a=single',
            params: {
                id: record.get("id")
            },
            failure: function (form, action) {
                Ext.Msg.alert("加载失败", action.result.errorMessage);
            }
        });
    },
    addAuthor: function (button) {
        var edit = Ext.create('yzmh.view.author.Edit').show();
    },
    saveAuthor: function(button) {
        var win    = button.up('window'),
            form   = win.down('form'),
            record = form.getRecord(),
            values = form.getValues(),
            store = this.getAuthorsStore();
        if (form.getForm().isValid()) {
            form.submit({
                url: 'data/author.aspx?a=save',
                success: function (form, action) {
                    win.close();
                    store.reload();
                },
                failure: function (form, action) {
                    Ext.Msg.alert('Failed', action.result ? action.result.message : 'No response');
                }
            });
        }
    },

    tjChange: function (o, item, rowIndex, colIndex, e, row) {
        var store = this.getAuthorsStore();
        Ext.Ajax.request({
            url: 'data/author.aspx?a=updatecolumn',
            params: { id: row.get('id'), column: 'tj', value: row.get('tj') ? 0 : 1 },
            method: 'POST',
            success: function (response, opts) {
                var result = Ext.decode(response.responseText);
                if (result.success) {
                    row.set("tj", opts.params.value);
                    store.sync();
                } else {
                    Ext.Msg.alert("错误", result.errorMessage);
                }
            }
        });
    },

    htmlAuthor: function () {
        var rows = this.getGrid().getSelectionModel().getSelection();
        if (rows.length == 0) {
            Ext.Msg.alert("提示", "请至少选择一行记录！");
            return;
        }
        var store = this.getAuthorsStore();
        var pbar = this.getProgressbar();
        var v = 0;
        function createZZ(record) {
            Ext.Ajax.request({
                url: 'data/makehtml.aspx?a=author',
                params: { id: record.get("id") },
                method: 'POST',
                success: function (response, opts) {
                    var result = Ext.decode(response.responseText);
                    if (result.success) {
                        v++;
                        pbar.updateProgress(v / rows.length, '生成HTML页面 ' + v + ' / ' + rows.length + '...');
                        if (rows[v] != null) {
                            createZZ(rows[v]);
                        } else {
                            pbar.updateText('生成HTML完成')
                        }
                    } else {
                        Ext.Msg.alert("错误", result.errorMessage);
                    }
                }
            });
        }
        Ext.Msg.confirm("提示", "确定要生成选中的" + rows.length + "个作者页吗？", function (result) {
            if (result == "yes") {
                pbar.reset();
                pbar.show();
                createZZ(rows[0])
            }
        });
    },

    htmlAllAuthor: function () {
        var rows = [];
        var store = this.getAuthorsStore();
        var pbar = this.getProgressbar();
        var v = 0;
        function createZZ(id) {
            Ext.Ajax.request({
                url: 'data/makehtml.aspx?a=author',
                params: { id: id },
                method: 'POST',
                success: function (response, opts) {
                    var result = Ext.decode(response.responseText);
                    if (result.success) {
                        v++;
                        pbar.updateProgress(v / rows.length, '生成HTML页面 ' + v + ' / ' + rows.length + '...');
                        if (rows[v] != null) {
                            createZZ(rows[v]);
                        } else {
                            pbar.updateText('全部作者页生成HTML完成')
                        }
                    } else {
                        Ext.Msg.alert("错误", result.errorMessage);
                    }
                }
            });
        }
        Ext.Msg.confirm("提示", "确定要生成选中的全部作者页面吗？", function (result) {
            if (result == "yes") {
                Ext.Ajax.request({
                    url: 'data/author.aspx?a=allid',
                    success: function (response, opts) {
                        var result = Ext.decode(response.responseText);
                        if (result.success) {
                            rows = result.allid;
                            pbar.reset();
                            pbar.show();
                            createZZ(rows[0])
                        }
                    }
                });

            }
        });
    }
});
